package com.example.leetcode.juc;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.IntConsumer;

class FizzBuzz {
    private int n;

    volatile int state = 1;
    ReentrantLock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    public FizzBuzz(int n) {
        this.n = n;
        state = 1;
    }

    // printFizz.run() outputs "fizz".
    public void fizz(Runnable printFizz) throws InterruptedException {
        for (int i = 1; i <= n; i++) {
            if (!(i % 3 == 0 && i % 5 != 0)){
                continue;
            }
            lock.lock();
            while (!(state % 3 == 0 && state % 5 != 0)) {
                condition.await();
            }
            printFizz.run();
            state++;
            condition.signalAll();
            lock.unlock();
        }
    }

    // printBuzz.run() outputs "buzz".
    public void buzz(Runnable printBuzz) throws InterruptedException {
        for (int i = 1; i <= n; i++) {
            if (!(i % 5 == 0 && i % 3 != 0)){
                continue;
            }
            lock.lock();
            while (!(state % 5 == 0 && state % 3 != 0)) {
                condition.await();
            }
            printBuzz.run();
            state++;
            condition.signalAll();
            lock.unlock();
        }
    }

    // printFizzBuzz.run() outputs "fizzbuzz".
    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
        for (int i = 1; i <= n; i++) {
            if (!(i % 5 == 0 && i % 3 == 0)){
                continue;
            }
            lock.lock();
            while (!(state % 5 == 0 && state % 3 == 0)) {
                condition.await();
            }
            printFizzBuzz.run();
            state++;
            condition.signalAll();
            lock.unlock();
        }
    }

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void number(IntConsumer printNumber) throws InterruptedException {
        for (int i = 1; i <= n; i++) {
            if (i % 5 == 0 || i % 3 == 0) {
                continue;
            }
            lock.lock();
            while (state % 5 == 0 || state % 3 == 0) {
                condition.await();
            }
            printNumber.accept(i);
            state++;
            condition.signalAll();
            lock.unlock();
        }
    }
}

public class leetcode1195 {
    public static void main(String[] args) {

        FizzBuzz fizzBuzz = new FizzBuzz(15);
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        Runnable task1 = new Runnable() {
            @Override
            public void run() {
                try {
                    fizzBuzz.fizz(new Runnable() {
                        @Override
                        public void run() {
                            System.out.println("fizz");
                        }
                    });
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        };

        Runnable task2 = new Runnable() {
            @Override
            public void run() {
                try {
                    fizzBuzz.buzz(new Runnable() {
                        @Override
                        public void run() {
                            System.out.println("buzz");
                        }
                    });
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        };

        Runnable task3 = new Runnable() {
            @Override
            public void run() {
                try {
                    fizzBuzz.fizzbuzz(new Runnable() {
                        @Override
                        public void run() {
                            System.out.println("fizzbuzz");
                        }
                    });
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        };

        Runnable task4 = new Runnable() {
            @Override
            public void run() {
                IntConsumer intConsumer = new IntConsumer() {
                    @Override
                    public void accept(int value) {
                        System.out.println(value);
                    }
                };
                try {
                    fizzBuzz.number(intConsumer);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        };

        executorService.execute(task1);
        executorService.execute(task2);
        executorService.execute(task3);
        executorService.execute(task4);

        executorService.shutdown();

    }
}
